package com.forever.toolAndAgent;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.model.openai.OpenAiChatModel;
import org.springframework.stereotype.Service;

@Service
public class ChatAssistantImpl implements ChatAssistant {
    ChatLanguageModel model =
            new OpenAiChatModel
                    .OpenAiChatModelBuilder()
                    .baseUrl("http://localhost:11434/v1")
                    .apiKey("ollama")
                    .modelName("qwen2:7b")
                    .logRequests(true)
                    .logResponses(true)
                    .build();
    ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(15);
    ChatAssistant assistant = AiServices.builder(ChatAssistant.class)
            .chatLanguageModel(model)
            .chatMemoryProvider(memoryId -> chatMemory)
            .build();

    @Override
    public String chat(Integer sessionId, String message) {
        return assistant.chat(sessionId, message);
    }
}
